import request from '@/utils/request'

// Chat page path for navigation
export const CHAT_PAGE = '/pages/consult/chat'

// 通过网关访问患者服务的问诊控制器前缀
// 与 Follow 模块一致走 /patient 前缀
export const CONSULT_PREFIX = '/patient/consult'

/**
 * Start a consultation with a doctor (deduct fee server-side)
 * POST /consult/consult/{doctorId}
 * @param {number|string} doctorId
 * @returns {Promise<any>} response data
 */
export function startConsult(doctorId) {
  const id = Number(String(doctorId).trim())
  return request({
    url: `${CONSULT_PREFIX}/consult/${id}`,
    method: 'post'
  })
}

/**
 * Add H-coin flow record
 * POST /consult/record
 * @param {object} flow
 */
export function recordFlow(flow) {
  return request({
    url: `${CONSULT_PREFIX}/record`,
    method: 'post',
    data: flow
  })
}

/**
 * Navigate to chat page with params
 * @param {{doctorId?: number|string, sessionId?: string|number, doctorName?: string, doctorAvatar?: string}} params
 */
export function goChat(params = {}) {
  const q = []
  if (params.doctorId != null) q.push(`doctorId=${encodeURIComponent(params.doctorId)}`)
  if (params.sessionId != null) q.push(`sessionId=${encodeURIComponent(params.sessionId)}`)
  if (params.doctorName != null) q.push(`doctorName=${encodeURIComponent(params.doctorName)}`)
  if (params.doctorAvatar != null) q.push(`doctorAvatar=${encodeURIComponent(params.doctorAvatar)}`)
  const url = `${CHAT_PAGE}${q.length ? ('?' + q.join('&')) : ''}`
  uni.navigateTo({ url })
}

export default { startConsult, recordFlow, goChat, CHAT_PAGE, CONSULT_PREFIX }
